

'use strict';
require('./index.css');
var _kit  = require('kit/kit.js');
var _user = require('service/user-service.js');
var _cart = require('service/cart-service.js');

var nav = {
  init : function(){
    this.bindEvent();
    this.loadUserInfo();
    this.loadCartCount();
    return this;
  },
  bindEvent : function(){
    $('.js-login').click(function(){
      _kit.doLogin();
    });
    $('.js-register').click(function(){
      window.location.href = './register.html'
    });
    $('.js-logout').click(function(){
      _user.logout(function(res){
        window.location.reload();
      },function(errMsg){
        _kit.errorTips(errMsg);
      });
    });
  },
  loadUserInfo : function(){
    _user.checkLogin(function(res){
      $('.user.login').hide().siblings('.user.logout').show().find('.username').text(res.username)
    },function(errMsg){
      // do not nothing
    });
  },
  loadCartCount : function(){
    _cart.getCartCount(function(res){
      $('.cart-count').text(res || 0);
    },function(errMsg){
      $('.cart-count').text(0);
    });
  }
};

module.exports = nav.init();
